home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Hackers Underworld 2: Forbidden Knowledge
/
Hackers Underworld 2: Forbidden Knowledge.iso
/
HACKING
/
UNIX_NEW.TXT
< prev
next >
Wrap
Internet Message Format
|
1994-07-17
|
32KB
From: sahayman@iuvax.cs.indiana.edu (Steve Hayman)
Newsgroups: comp.unix.questions,comp.unix.wizards
Subject: Frequently Asked Questions about Unix - with Answers [Monthly posting]
Message-ID: <FAQ2.9@iuvax.cs.indiana.edu>
Date: 1 Dec 89 19:54:14 GMT
Expires: 1 Jan 90 05:00:00 GMT
Followup-To: comp.unix.questions
Organization: Computer Science Department, Indiana University
Lines: 865
Supersedes: <FAQ2.8@iuvax.cs.indiana.edu>
[Last changed: $Date: 89/12/01 14:50:10 $ by $Author: sahayman $]
This article contains the answers to some Frequently Asked Questions
often seen in comp.unix.questions and comp.unix.wizards. Please don't
ask these questions again, they've been answered plenty of times
already - and please don't flame someone just because they may not have
read this particular posting. Thank you.
This article includes answers to:
How do I remove a file whose name begins with a "-" ?
How do I remove a file with funny characters in the filename ?
How do I get a recursive directory listing?
How do I get the current directory into my prompt?
How do I read characters from a terminal without requiring the user
to hit RETURN?
How do I read characters from the terminal in a shell script?
How do I check to see if there are characters to be read without
actually reading?
How do I find the name of an open file?
How do I rename "*.foo" to "*.bar", or change file names to lowercase?
Why do I get [some strange error message] when I "rsh host command" ?
How do I find out the creation time of a file?
How do I use "rsh" without having the rsh hang around
until the remote command has completed?
How do I truncate a file?
How do I {set an environment variable, change directory} inside a
shell script and have that change affect my current shell?
Why doesn't find's "{}" symbol do what I want?
How do I redirect stdout and stderr separately in csh?
How do I set the permissions on a symbolic link?
What does {awk,grep,fgrep,egrep,biff,cat,gecos,nroff,troff,tee,bss}
stand for?
How do I pronounce "vi" , or "!", or "/*", or ...?
While these are all legitimate questions, they seem to crop up in
comp.unix.questions on an annual basis, usually followed by plenty
of replies (only some of which are correct) and then a period of
griping about how the same questions keep coming up. You may also like
to read the monthly article "Answers to Frequently Asked Questions"
in the newsgroup "news.announce.newusers", which will tell you what
"UNIX" stands for.
With the variety of Unix systems in the world, it's hard to guarantee
that these answers will work everywhere. Read your local manual pages
before trying anything suggested here. If you have suggestions or
corrections for any of these answers, please send them to to
sahayman@iuvax.cs.indiana.edu or iuvax!sahayman.
1) How do I remove a file whose name begins with a "-" ?
Figure out some way to name the file so that it doesn't
begin with a dash. The simplest answer is to use
rm ./-filename
(assuming "-filename" is in the current directory, of course.)
This method of avoiding the interpretation of the "-" works
with other commands too.
Many commands, particularly those that have been written to use
the "getopt(3)" argument parsing routine, accept a "--" argument
which means "this is the last option, anything after this is not
an option", so your version of rm might handle "rm -- -filename".
Some versions of rm that don't use getopt() treat a single "-"
in the same way, so you can also try "rm - -filename".
2) How do I remove a file with funny characters in the filename ?
The classic answers are
rm -i some*pattern*that*matches*only*the*file*you*want
which asks you whether you want to remove each file matching
the indicated pattern; depending on your shell, this may
not work if the filename has a character with the 8th bit set
(the shell may strip that off);
and
rm -ri .
which asks you whether to remove each file in the directory,
answer "y" to the problem file and "n" to everything else.,
and which, unfortunately, doesn't work with many versions of rm;
(always take a deep breath and think about what you're doing
and double check what you typed when you use rm's "-r" flag)
and
find . -type f ... -ok rm '{}' \;
where "..." is a group of predicates that uniquely identify the
file. One possibility is to figure out the inode number
of the problem file (use "ls -i .") and then use
find . -inum 12345 -ok rm '{}' \;
or
find . -inum 12345 -ok mv '{}' new-file-name \;
"-ok" is a safety check - it will prompt you for confirmation of the
command it's about to execute. You can use "-exec" instead to avoid
the prompting, if you want to live dangerously, or if you suspect
that the filename may contain a funny character sequence that will mess
up your screen when printed.
If none of these work, find your system manager.
3) How do I get a recursive directory listing?
One of the following may do what you want:
ls -R (not all versions of "ls" have -R)
find . -print (should work everywhere)
du -a . (shows you both the name and size)
If you're looking for a wildcard pattern that will match
all ".c" files in this directory and below, you won't find one,
but you can use
% some-command `find . -name '*.c' -print`
"find" is a powerful program. Learn about it.
4) How do I get the current directory into my prompt?
It depends which shell you are using. It's easy with some shells,
hard or impossible with others.
C Shell (csh):
Put this in your .cshrc - customize the prompt variable
the way you want.
alias setprompt 'set prompt="${cwd}% "'
setprompt # to set the initial prompt
alias cd 'chdir \!* && setprompt'
If you use pushd and popd, you'll also need
alias pushd 'pushd \!* && setprompt'
alias popd 'popd \!* && setprompt'
Some C shells don't keep a $cwd variable - you can use
`pwd` instead.
If you just want the last component of the current directory
in your prompt ("mail% " instead of "/usr/spool/mail% ")
you can use
alias setprompt 'set prompt="$cwd:t% "'
Some older csh's get the meaning of && and || reversed.
Try doing:
false && echo bug
If it prints "bug", you need to switch && and || (and get
a better version of csh.)
Bourne Shell (sh):
If you have a newer version of the Bourne Shell (SVR2 or newer)
you can use a shell function to make your own command, "xcd" say:
xcd() { cd $* ; PS1="`pwd` $ "; }
If you have an older Bourne shell, it's complicated but not impossible.
Here's one way. Add this to your .profile file:
LOGIN_SHELL=$$ export LOGIN_SHELL
CMDFILE=/tmp/cd.$$ export CMDFILE
PROMPTSIG=16 export PROMPTSIG
trap '. $CMDFILE' $PROMPTSIG
and then put this executable script (without the indentation!),
let's call it "xcd", somewhere in your PATH
: xcd directory - change directory and set prompt
: by signalling the login shell to read a command file
cat >${CMDFILE?"not set"} <<EOF
cd $1
PS1="\`pwd\`$ "
EOF
kill -${PROMPTSIG?"not set"} ${LOGIN_SHELL?"not set"}
Now change directories with "xcd /some/dir".
Korn Shell (ksh):
Put this in your .profile file:
PS1='$PWD $ '
If you just want the last component of the directory, use
PS1='${PWD##*/} $ '
5) How do I read characters from a terminal without requiring the user
to hit RETURN?
Check out cbreak mode in BSD, ~ICANON mode in SysV.
If you don't want to tackle setting the terminal parameters
yourself (using the "ioctl(2)" system call) you can let the stty
program do the work - but this is slow and inefficient, and you
should change the code to do it right some time:
main()
{
int c;
printf("Hit any character to continue\n");
/*